package com.fhzz.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fhzz.service.LoginService;

@Controller
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	@RequestMapping("login")
	public String toLogin() {
		return "login";
	}
	
	@RequestMapping("/")
	public String toLogin2() {
		return "login";
	}
	
	@PostMapping(value="/loginvalidate")
	public String loginvalidate(@RequestParam("username") String username,
			@RequestParam("password") String pwd,HttpSession httpSession){
		if(username==null) {
			return "login";
		}
		String realpwd=loginService.getpwdbyname(username);
		if(realpwd!=null&&pwd.equals(realpwd)){
			httpSession.setAttribute("username", username);
			return "index";
		}else
			return "fail";
	}
}
